#include <linux/init.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/kobject.h>


// 定义2个kobject指针变量
struct kobject *mykobject01;
struct kobject *mykobject02;

struct kset *mykset;

// //定义了一个kobj_type结构体变量, 用于描述kobject类型
struct kobj_type mykobj_type;



static int __init mykobject_init(void)
{   
    int ret;
    printk(KERN_EMERG "mykobject_init OK!\n");

    mykset = kset_create_and_add("mykset", NULL, NULL);

    mykobject01 = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    mykobject01->kset = mykset;
    ret = kobject_init_and_add(mykobject01, &mykobj_type, NULL, "%s", "mykobject01");

    mykobject02 = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    mykobject02->kset = mykset;
    ret = kobject_init_and_add(mykobject02, &mykobj_type, NULL, "%s", "mykobject02");

    return 0;
}



static void __exit mykojbect_exit(void)
{
    // 释放之前创建的kobject对象
    kobject_put(mykobject02);
    kobject_put(mykobject01);
    // 释放kset
    kset_put(mykset);
    printk(KERN_EMERG "mykojbect_exit OK!\n");  
}



module_init(mykobject_init);
module_exit(mykojbect_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Treasure1029");
